package com.ruoyi.web.controller.aggregation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.aggregation.Card;
import com.ruoyi.system.service.aggregation.ICardService;

/**
 * 银行卡Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/card")
public class CardController
{
    @Autowired
    private ICardService cardService;

    /**
     * 新增银行卡
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Card card)
    {
        try
        {
            cardService.insertCard(card);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 编辑银行卡
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Card card)
    {
        try
        {
            cardService.updateCard(card);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 银行卡详情
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam(required = false) Long userId)
    {
        try
        {
            Long targetUserId = userId;
            if (targetUserId == null) {
                // 没传userId，获取当前登录用户的银行卡
                targetUserId = SecurityUtils.getUserId();
                if (targetUserId == null) {
                    return AjaxResult.error("用户未登录");
                }
            }
            Card card = cardService.selectCardByUserId(targetUserId);
            return AjaxResult.success(card);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取当前登录用户的银行卡
     */
    @GetMapping("/current")
    public AjaxResult getCurrentCard()
    {
        try
        {
            Long currentUserId = SecurityUtils.getUserId();
            if (currentUserId == null) {
                return AjaxResult.error("用户未登录");
            }
            Card card = cardService.selectCardByUserId(currentUserId);
            return AjaxResult.success(card);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

